package com.yaorange.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Apply;
import com.yaorange.mapper.ApplyMapper;
import com.yaorange.mapper.ArticleMapper;
import com.yaorange.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: CorJ
 * @version: 1.0
 * @create: 2021-12-01 14:52
 */
@Service
@Transactional
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public PageInfo<Apply> getPageLst(Integer pageNum, Integer pageSize, String articleName,String applyDate,String applyName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Apply> applyList = applyMapper.selectApply(articleName,applyDate,applyName);
        return new PageInfo<>(applyList);
    }

    @Override
    public boolean updateById(Integer applyId, Integer stateId,Integer articleId,Integer applyNum) {
        int rows = applyMapper.updateById(applyId,stateId);
        if(rows>0){
        /*    articleMapper.updateNumAddById(articleId,applyNum);*/
            return true;
        }
        return false;
    }

    @Override
    public boolean updateMoreById(List<Integer> ids, Integer stateId) {
        int rows = applyMapper.updateMoreById(ids,stateId);
        if(rows>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean addApply(Apply apply,Integer id) {
        int rows = applyMapper.insertApply(apply);
        int num = apply.getApplyNum();
        articleMapper.updateNumById(num,id);
        if(rows>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Apply> getStateList(Integer id) {
        return applyMapper.selectStateList(id);
    }

    @Override
    public PageInfo<Apply> getList(Integer pageNum, Integer pageSize, Integer stateId, String applyDate, Integer userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Apply> applyList = applyMapper.selectApplyList(stateId,applyDate,userId);
        return new PageInfo<>(applyList);
    }

    @Override
    public boolean update(Integer applyId, Integer applyNum,Integer articleId) {
        int stateId = 2;
       int rows = applyMapper.updateState(applyId,stateId);
       if(rows>0){
       articleMapper.updateNumAddById(articleId,applyNum);
       return true;
       }
        return false;
    }

    @Override
    public Apply getApply(Integer aId) {
        return applyMapper.selectApplyById(aId);
    }

    @Override
    public boolean updateSub(Integer applyId, Integer applyNum, Integer articleId) {
        int stateId = 0;
        int rows = applyMapper.updateState(applyId,stateId);
        if(rows>0){

            return true;
        }
        return false;

    }

    @Override
    public boolean updateApply(Integer applyId, String applyReason, String applyDate, Integer applyNum) {
        Integer stateId = 0;
       int rows = applyMapper.updateApply(applyId,applyReason,applyDate,applyNum,stateId);
        if(rows>0){
            return true;
        }
       return false;
    }
}
